package tw.com.msig.b2c.car.product.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.math.NumberUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import tw.com.msig.b2c.car.common.web.APILibDirector;
import tw.com.msig.b2c.car.common.web.BaseAction;
import tw.com.msig.b2c.car.product.service.PdtInsItemService;
import tw.com.msig.b2c.car.product.vo.PdtInsItemVo;

public class PdtInsItemAction extends BaseAction{

	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		String forwardPage = "list";
		String sActionType = request.getParameter("sActionType");

		if ("list".equals(sActionType)) {
			forwardPage = list(request, response);
		} else if ("add".equals(sActionType)) {
			forwardPage = add(request, response);
		} else if ("edit".equals(sActionType)) {
			forwardPage = edit(request, response);
		} else if ("delete".equals(sActionType)) {
			forwardPage = delete(request, response);
		} else if ("insert".equals(sActionType)) {
			forwardPage = insert(request, response);
		} else if ("update".equals(sActionType)) {
			forwardPage = update(request, response);
		}
		return mapping.findForward(forwardPage);
	}

	private String update(HttpServletRequest request, HttpServletResponse response) {
		PdtInsItemService pdtInsItemService = (PdtInsItemService) APILibDirector.getSpringBean("pdtInsItemService");
		PdtInsItemVo insItemVo = new PdtInsItemVo();
		insItemVo.setId(NumberUtils.toLong(request.getParameter("id")));
		insItemVo.setCode(request.getParameter("code"));
		insItemVo.setDescription(request.getParameter("description"));
		insItemVo.setAmounts(request.getParameter("amounts"));

		pdtInsItemService.update(insItemVo);
		return list(request, response);
	}

	private String insert(HttpServletRequest request, HttpServletResponse response) {
		PdtInsItemService pdtInsItemService = (PdtInsItemService) APILibDirector.getSpringBean("pdtInsItemService");
		PdtInsItemVo insItemVo = new PdtInsItemVo();
		insItemVo.setCode(request.getParameter("code"));
		insItemVo.setDescription(request.getParameter("description"));
		insItemVo.setAmounts(request.getParameter("amounts"));

		pdtInsItemService.insert(insItemVo);

		return list(request, response);
	}

	private String delete(HttpServletRequest request, HttpServletResponse response) {
		PdtInsItemService pdtInsItemService = (PdtInsItemService) APILibDirector.getSpringBean("pdtInsItemService");
		log.debug("{}", request.getParameter("id"));
		PdtInsItemVo insItemVo = new PdtInsItemVo();
		insItemVo.setId(NumberUtils.toLong(request.getParameter("id")));

		pdtInsItemService.delete(insItemVo);
		return list(request, response);
	}

	private String edit(HttpServletRequest request, HttpServletResponse response) {
		PdtInsItemService pdtInsItemService = (PdtInsItemService) APILibDirector.getSpringBean("pdtInsItemService");
		Long id = NumberUtils.toLong(request.getParameter("id"));
		log.debug("{}",id);
		PdtInsItemVo insItemVo = pdtInsItemService.getById(id);
		log.debug("{}",insItemVo);
		request.setAttribute("insItem", insItemVo);
		return "edit";
	}

	private String add(HttpServletRequest request, HttpServletResponse response) {
		return "add";
	}

	public String list(HttpServletRequest request, HttpServletResponse response) {
		PdtInsItemService pdtInsItemService = (PdtInsItemService) APILibDirector.getSpringBean("pdtInsItemService");
		List<PdtInsItemVo> insItemVos = pdtInsItemService.getAll();
		request.setAttribute("models", insItemVos);
		log.debug("----------------" + insItemVos.toString() + "---------------------------");
		return "list";
	}
}
